// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const TcbRouter = require('tcb-router')
// 初始化数据库
const db = cloud.database()

// 获取blog数据库集合
const blogCollection  = db.collection('blog')

// 云函数入口函数
exports.main = async (event, context) => {
  const app = new TcbRouter({event})

  app.router('list',async (ctx,next)=>{
    const keywords = event.keywords
    // 查询条件
    let w = {}
    if(keywords.trim() != ''){
      w = {
        content:db.RegExp({
          regexp:keywords,
          options:'i'
        })
      }
    }

    let bloglist = await blogCollection.where(w).skip(event.start)
    .limit(event.count)
    .orderBy('createTime','desc')
    .get()
    .then(res => {
      return res.data
    })
    ctx.body = bloglist
  })

  app.router('detail', async (ctx, next) => {
    let blogId = event.blogId
    // // 详情查询
    // let detail = await blogCollection.where({
    //   _id: blogId
    // }).get().then((res) => {
    //   return res.data
    // })
    // // 评论查询
    // const countResult = await db.collection('blog-comment').count()
    // const total = countResult.total
    // let commentList = {
    //   data: []
    // }
    // if (total > 0) {
    //   const batchTimes = Math.ceil(total / MAX_LIMIT)
    //   const tasks = []
    //   for (let i = 0; i < batchTimes; i++) {
    //     let promise = db.collection('blog-comment').skip(i * MAX_LIMIT)
    //       .limit(MAX_LIMIT).where({
    //         blogId
    //       }).orderBy('createTime', 'desc').get()
    //     tasks.push(promise)
    //   }
    //   if (tasks.length > 0) {
    //     commentList = (await Promise.all(tasks)).reduce((acc, cur) => {
    //       return {
    //         data: acc.data.concat(cur.data)
    //       }
    //     })
    //   }

    // }

    // ctx.body = {
    //   commentList,
    //   detail,
    // }

    // 聚合查询
    const blog = await blogCollection.aggregate().match({
      _id: blogId
    }).lookup({
      from: 'blog-comment',
      localField: '_id',
      foreignField: 'blogId',
      as: 'commentList'
    }).end()
    ctx.body = blog
  })

  const wxContent = cloud.getWXContext()

  app.router('getListByOpenid',async (ctx, next) => {
    ctx.body = await blogCollection.where({
      _openid:wxContent.OPENID
    }).skip(event.start).limit(event.count)
    .orderBy('createTime','desc').get().then(res => {
      return res.data
    })
  })

  return app.serve()
}